package com.momo.hr.service;


import com.momo.hr.bean.Employee;
import com.momo.hr.bean.MailConstants;
import com.momo.hr.bean.MailSendLog;
import com.momo.hr.bean.RespPageBean;
import com.momo.hr.mapper.EmployeeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class EmployeeService {

    Logger logger = LoggerFactory.getLogger(EmployeeService.class);

    @Autowired
    EmployeeMapper employeeMapper;

    //RabbitTemplate 是一个为我们准备号的消息队列中的模板，用来发送消息的
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    MailSendLogService mailSendLogService;

    SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
    SimpleDateFormat monFormat = new SimpleDateFormat("MM");
    //格式化为数字类型
    DecimalFormat decimalFormat = new DecimalFormat("##.00");

    public RespPageBean getEmployeeByPage(Integer page, Integer size, Employee employee, Date[] beginDateScope) {
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        List<Employee> data = employeeMapper.getEmployeeByPage(page, size, employee, beginDateScope);
        Long total = employeeMapper.getTotal(employee, beginDateScope);
        RespPageBean bean = new RespPageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }

    public int addEmp(Employee employee) {
        //添加员工的时候把合同事件算出来
        Date beginContract = employee.getBeginContract();
        Date endContract = employee.getEndContract();
        //[格式化后的结束年份] - [格式化后的开始年份] ）* 12 +（[格式化后的结束月份] - [格式化后的开始月份]） / 12
        double v = (Double.parseDouble(yearFormat.format(endContract))
                -
                Double.parseDouble(yearFormat.format(beginContract)))
                * 12
                +
                (Double.parseDouble(monFormat.format(endContract))
                -
                Double.parseDouble(monFormat.format(beginContract)));
        //然后把计算出事件 使用Decimal格式化以后
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(v / 12)));
        int result = this.employeeMapper.insertSelective(employee);
        //查询完整信
        if(result == 1){
            Employee employeeById = employeeMapper.getEmployeeById(employee.getId());
            /**
             * 这里做了消息的可靠性处理
             */
            //开始设置发送的信息
            String msgId = UUID.randomUUID().toString();
            MailSendLog mailSendLog = new MailSendLog();
            mailSendLog.setMsgId(msgId);
            mailSendLog.setCreateTime(new Date());
            mailSendLog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
            mailSendLog.setRouteKey(MailConstants.MAIL_ROUTING_KEY_NAME);
            mailSendLog.setEmpId(employeeById.getId());
            mailSendLog.setTryTime(
                    new Date(System.currentTimeMillis()
                            +1000
                            *60
                            *MailConstants.MSG_TIMEOUT));//当前时间的 1分钟以后
            mailSendLogService.insert(mailSendLog);//数据库中插入对应的数据
            //去调用我们自定义好的rabbitTemplate来是实现发送信息
            this.rabbitTemplate.convertAndSend(
                    MailConstants.MAIL_EXCHANGE_NAME,//把消息发送给交换机
                    MailConstants.MAIL_ROUTING_KEY_NAME,//路由名称
                    employeeById,//发送的数据
                    new CorrelationData(msgId));//消息的唯一表示，这里我们是以UUID来使用的

            logger.info(employeeById.toString());//答应出相关信息
        }
        return result;
    }


    public Integer getMaxWorkId(){
        return this.employeeMapper.maxWorkID();
    }

    public int deleteEmpById(Integer id) {
        return this.employeeMapper.deleteByPrimaryKey(id);
    }

    public int updateEmp(Employee emp) {
        return this.employeeMapper.updateByPrimaryKeySelective(emp);
    }

    public int addEmps(List<Employee> employees) {
        return this.employeeMapper.addEmps(employees);
    }

    public Employee getEmployeeById(Integer empId) {
        return this.employeeMapper.getEmployeeById(empId);
    }
}

